package com.ruoyi.shopping_products.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.shopping_products.domain.PdPdPool;
import com.ruoyi.shopping_products.mapper.PdDetailMapper;
import com.ruoyi.shopping_products.domain.PdDetail;
import com.ruoyi.shopping_products.service.IPdDetailService;

/**
 * 商品详细信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-10-28
 */
@Service
public class PdDetailServiceImpl implements IPdDetailService 
{
    @Autowired
    private PdDetailMapper pdDetailMapper;

    /**
     * 查询商品详细信息
     * 
     * @param id 商品详细信息主键
     * @return 商品详细信息
     */
    @Override
    public PdDetail selectPdDetailById(Long id)
    {
        return pdDetailMapper.selectPdDetailById(id);
    }

    /**
     * 查询商品详细信息列表
     * 
     * @param pdDetail 商品详细信息
     * @return 商品详细信息
     */
    @Override
    public List<PdDetail> selectPdDetailList(PdDetail pdDetail)
    {
        return pdDetailMapper.selectPdDetailList(pdDetail);
    }

    /**
     * 新增商品详细信息
     * 
     * @param pdDetail 商品详细信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPdDetail(PdDetail pdDetail)
    {
        int rows = pdDetailMapper.insertPdDetail(pdDetail);
        insertPdPdPool(pdDetail);
        return rows;
    }

    /**
     * 修改商品详细信息
     * 
     * @param pdDetail 商品详细信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePdDetail(PdDetail pdDetail)
    {
        pdDetailMapper.deletePdPdPoolByPdId(pdDetail.getId());
        insertPdPdPool(pdDetail);
        return pdDetailMapper.updatePdDetail(pdDetail);
    }

    /**
     * 批量删除商品详细信息
     * 
     * @param ids 需要删除的商品详细信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePdDetailByIds(Long[] ids)
    {
        pdDetailMapper.deletePdPdPoolByPdIds(ids);
        return pdDetailMapper.deletePdDetailByIds(ids);
    }

    /**
     * 删除商品详细信息信息
     * 
     * @param id 商品详细信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePdDetailById(Long id)
    {
        pdDetailMapper.deletePdPdPoolByPdId(id);
        return pdDetailMapper.deletePdDetailById(id);
    }

    /**
     * 新增用户商品关联信息
     * 
     * @param pdDetail 商品详细信息对象
     */
    public void insertPdPdPool(PdDetail pdDetail)
    {
        List<PdPdPool> pdPdPoolList = pdDetail.getPdPdPoolList();
        Long id = pdDetail.getId();
        if (StringUtils.isNotNull(pdPdPoolList))
        {
            List<PdPdPool> list = new ArrayList<PdPdPool>();
            for (PdPdPool pdPdPool : pdPdPoolList)
            {
                pdPdPool.setPdId(id);
                list.add(pdPdPool);
            }
            if (list.size() > 0)
            {
                pdDetailMapper.batchPdPdPool(list);
            }
        }
    }
}
